package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    //插入订单数据
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Select("select id from orders where number = #{number}")
    Long getIdbyNumber(String number);

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus},checkout_time=#{checkOutTime} where id = #{orderid}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, Long orderid);

    Page<Orders> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    //接单
    @Update("update orders set status = 3 where id = #{id}")
    void confirm(Long id);

    //派送
    @Update("update orders set status = 4 where id = #{id}")
    void delivery(Long id);

    //完成订单
    @Update("update orders set status = 5 where id = #{id}")
    void complete(Long id);

    @Select("SELECT * from orders where id = #{id}")
    Orders getById(Long id);

    //拒单
    @Update("update orders set rejection_reason = #{rejectionReason} , status = 7 where id = #{id}")
    void rejection(Orders orders);

    //取消
    @Update("update orders set cancel_reason = #{cancelReason} , status = 6 ,cancel_time = #{cancelTime} where id = #{id}")
    void cancel(Orders orders);

    @Select("select count(id) from orders where status = #{status}")
    Integer getOrderNumber(Integer status);

    //历史订单查询

    Page<Orders> historyOrders(Integer status,Long id);


    //根据订单状态和下单时间查询订单
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status,LocalDateTime orderTime);

    //营业额统计
    Double sumByMap(Map map);

    Integer getUserStatistics(Map map);

    Integer getDayOrderNumber(Map map);

    List<GoodsSalesDTO> getSalesTop(LocalDateTime begin, LocalDateTime end);

    /**
     * 根据条件统计菜品数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
